var SportSelectionScene = function SportSelectionScene( camera, scene, render )
{
	Abstract3DScene.call( this, camera, scene, render );
	
	this.init = function init()
	{
		sportScene = this;
		this.initUI();
		this.loadModel();
		this.action = "";
	}
	
	this.initUI = function(){
		$("#selection a img").mouseenter(function(e){
			TweenLite.to($(this), .25, {css:{width:50, height:49}, ease:Quad.easeOut});
			TweenLite.to($(this).parent().parent().parent().find("#"+$(this).attr("alt")+"Info"),.6,{css:{opacity:1, top:95}});
		});
		$("#selection a img").mouseleave(function(e){
			TweenLite.to($(this), .25,{css:{width:45, height:44}, ease:Quad.easeIn});
			TweenLite.to($(this).parent().parent().parent().find("#"+$(this).attr("alt")+"Info"),.5,{css:{opacity:0, top:110}});
		});		
		$("#selection a").click(function(e){
			e.preventDefault();
			TweenLite.to(sportScene.base.position, 1.2, {y: 170, ease:Quad.easeIn});
			$("#selection").fadeOut('slow', function(){
				$("#loader").fadeIn('slow');
				$('#back').fadeOut('slow');
				if(RessourceManager.getInstance().percent<1){
					RessourceManager.getInstance().onComplete = sportScene.onImageLoadComplete;
					RessourceManager.getInstance().onProgress = sportScene.onImageLoadProgress;
					TweenLite.to($('#loader div div'), .4, {css:{height:parseInt(82*RessourceManager.getInstance().percent)+"px"}, ease:Linear.easeNone});
				} else {
					sportScene.onImageLoadComplete();
				}
			});
		});
	}
	
	this.loadModel = function()
	{
		modelLoader.onProggress = sportScene.onProgressAvatar;
		modelLoader.load({name:"head", model:"./dae/head.dae", scale:0.4});
		modelLoader.load({name:"leg", model:"./dae/leg.dae", scale:0.4});
		modelLoader.load({name:"armRight", model:"./dae/armRight.dae", scale:0.4});
		modelLoader.load({name:"armLeft", model:"./dae/armLeft.dae", scale:0.4});
		modelLoader.load({name:"miniPlatform", model:"./dae/decor_avatar.dae", scale:0.2});
		//modelLoader.loadJS({name:"miniPlatform", model:"./dae/decor_avatar.js", scale:0.2});
		modelLoader.load({name:"headLow", model:"./dae/headLow.dae", scale:0.4});
		modelLoader.load({name:"legLow", model:"./dae/legLow.dae", scale:0.4});
		modelLoader.load({name:"armRightLow", model:"./dae/armRightLow.dae", scale:0.4});
		modelLoader.load({name:"armLeftLow", model:"./dae/armLeftLow.dae", scale:0.4});
		modelLoader.load({name:"decor_sprint", model:"./dae/decor_sprint.dae", scale:6});
		//modelLoader.loadJS({name:"decor_sprint", model:"./dae/decor_sprint.js", scale:6});
	}
	
	this.onProgressAvatar = function(loaded,total)
	{
		//wait load is complete
		if(loaded/total<1){ 
			return;
		}
		
		//then load image for avatar
		$.getImageData({
			url: user.profile_image_url,
			server: URL_SERVER,
			success: function(image){
				sportScene.onLoadAvatarSuccess(image);
			},
			error: function(xhr, text_status){ 
				$.getImageData({
					url: user.profile_image_url,
					success: function(image)
					{
						sportScene.onLoadAvatarSuccess(image);
					},
					error: function(xhr, text_status){ 
					
					}
				});
			}
		});
	}
	
	this.onLoadAvatarSuccess = function(image)
	{
		imageUser = image;
		addPointLight(0xffffff,1.9,200,90,50,130);
		addPointLight(0xffffff,1.8,150,-50,50,70);
		
		var m = userMobile = new TO.Mobile(image);
			m.scale.x = 0.000001;
			m.scale.y = 0.000001;
			m.scale.z = 0.000001;
			m.position.y = 20;
			m.position.x = -19;
			m.vx = 6;
		
		var dae = modelLoader.get('miniPlatform');
		
		var obj = sportScene.base = new THREE.Object3D();
			obj.add(dae);
			obj.add(m);
			obj.position.y = -80;
			obj.position.x = 11;
		scene.add(obj);
		
		var finalScale = 0.33;
		var baseY = 22;
		
		TweenLite.to(obj.position, 1.3, { y:baseY, ease:Quad.easeOut});
		TweenLite.to(m.scale, .5, { delay:.5, y:finalScale, x:finalScale, z:finalScale, ease:Back.easeOut});
		TweenLite.to(m.position, .3, { delay:.5, y:m.position.y+15, ease:Quad.easeOut});
		TweenLite.to(m.position, .3, { delay:.8, y:-14, ease:Quad.easeIn, overwrite:"none"});
		TweenLite.to(m.scale, .3, { delay:1.1, y:finalScale*.7, ease:Quad.easeOut, overwrite:"none"});
		TweenLite.to(m.scale, .8, { delay:1.4, y:finalScale, ease:Back.easeOut, overwrite:"none"});
		TweenLite.to(obj.position, .3, { delay:1.1, y:baseY-10, ease:Quad.easeOut, overwrite:"none"});
		TweenLite.to(obj.position, .6, { delay:1.4, y:baseY, ease:Back.easeOut, overwrite:"none"});
		
		$(container).click(function(e){
			e.stopImmediatePropagation();
			var posX = $(this).position().left,posY = $(this).position().top-44;
			var vector = new THREE.Vector3( ( (e.pageX - posX) / $(this).width() ) * 2 - 1, - ( (e.pageY - posY) / $(this).height() ) * 2 + 1, 0.5 );
			projector.unprojectVector( vector, camera );
			var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
			if( ray.intersectObjects( [m] )){
				m.crazy();
			}
		});
		
		RessourceManager.getInstance().load(null, null);
		
		sportScene.changeLight();
		$('#selection').show();
		TweenLite.from($('#selection'),.6,{css:{opacity:0, height:'115px'},delay:.7},{css:{opacity:1, height:'135px'}});
		$('#back').fadeIn('slow');
		$('#back').click(function(e){
			e.preventDefault();
			$('#back').fadeOut('slow');
			tweenMaterial(sportScene.base, .5, {opacity: 0, ease:Quad.easeOut});
			$("#selection").fadeOut('slow', function(){
				removeAllLight();
				scene.remove(sportScene.base);
				sportScene.base = null;
				SceneTraveler.getInstance().transitionTo( new ConnexionScene(camera, scene, render) );
			});
		});
	}
	
	this.changeLight = function () {
		TweenLite.to(lights[0],1+Math.random()*1,{intensity:1.8+Math.random()*.6});
		TweenLite.to(lights[1],1+Math.random()*1,{intensity:1.8+Math.random()*.6, onComplete:sportScene.changeLight});
	}

	this.onImageLoadProgress = function (percent)
	{
		if(percent <1)
			TweenLite.to($('#loader div div'), .4, {css:{height:parseInt(82*percent)+"px"}, ease:Linear.easeNone});
	}

	this.onImageLoadComplete = function ()
	{
		TweenLite.killTweensOf($('#loader div div'));
		TweenLite.to($('#loader div div'), .25, {css:{height:"82px"}, onComplete:sportScene.onTweenLoaderComplete, ease:Linear.easeNone});
	}

	this.onTweenLoaderComplete = function ()
	{
		for(var i=1; i < 6; i++){
			TweenLite.to($("#cloud"+i),.5+Math.random()*.5,{delay:Math.random()*.3,css:{"opacity":0}});
		}
		TweenLite.killTweensOf(lights[0]);
		TweenLite.killTweensOf(lights[1]);
		$('#loader').fadeOut('slow', function()
		{
			removeAllLight();
			scene.remove(sportScene.base);
			sportScene.base = null;
			SceneTraveler.getInstance().transitionTo( new RaceScene(camera, scene, render) );
		});
	}

	this.update = function()
	{
		this.clock += .05;
		
		moveCloud2D($('#cloud1'),0,-.5);
		moveCloud2D($('#cloud2'),1,-.5);
		moveCloud2D($('#cloud3'),2,-.55);
		moveCloud2D($('#cloud4'),3,-.4);
		moveCloud2D($('#cloud5'),4,-.6);
		
		camera.position.x += ( mouseX/screenWidth * 45 - camera.position.x ) * .04;
		camera.position.y += ( 15-mouseY/screenHeight * 35 - camera.position.y ) * .04;
		camera.lookAt( scene.position );
		
		if(userMobile){
			//userMobile.head.rotation.y = ( mouseX/screenWidth * Math.PI/2 -Math.PI )+Math.PI;
			//userMobile.mesh.rotation.y = ( mouseX/screenWidth * Math.PI/2 -Math.PI )+Math.PI;
			//userMobile.head.rotation.z = ( (mouseY/screenHeight) * Math.PI/8 );
			//userMobile.mesh.rotation.z = ( (mouseY/screenHeight) * Math.PI/8 );
		}
		
	}
	
	this.init();
}
SportSelectionScene.prototype = new Abstract3DScene();
SportSelectionScene.prototype.constructor = SportSelectionScene;

var sportScene;